<?php
namespace backend\controllers;
use bc\controller\BackendController;
use bc\model\wc\AgentModel;
use bc\util\backend\Page;
use bc\model\source\StatisticsModel;

class AgentController extends BackendController
{
	
	//列表页面
	public function actionIndex()
	{
		$agentModel = new AgentModel();
		$params = '1 = 1 ';

		$model = new StatisticsModel ();
		
		$list = $agentModel->getAgentByUser_Users();
		
		foreach ($list as &$item)
		{
			if($item['scene_id'] > 0)
			{
				$sceneRow =	$model->getRowById($item['scene_id']);
				$item['qrcode_file'] = $sceneRow['qrcode_file'];
			}
		}
		
		$page = $this->get('page', 1);
		$agentModel->sort = 't_sort DESC';
		
		$total = $agentModel->getTotal($params);
		$pagination = new Page($total, self::PAGE_SIZE);
		$data['pagination'] = $pagination;
		$data['list'] = $list;
		return $this->render('index', $data);
	}
	//审核页面
	public function actionEdit()
	{
		$agentModel = new AgentModel();
		$id = $this->get('id');
		$info['list'] = $agentModel->getPresentInfo($id);
		return $this->render('edit',$info);
	}

	//
	public function actionInfo()
	{
		$agentModel = new AgentModel();
		$id = $this->post('id');
		$reply_status = $this->post('reply_status');
		$mark = $this->post('mark');
		$agentModel->modify([
			'reply_status'=>$reply_status,
			'mark'=>$mark,
			'reply_time'=>time(),
		],$id);
		
		if($reply_status == 1)
		{
			$row = $agentModel->getRowById($id);
			//审核通过发送二维码
			$model = new StatisticsModel ();
			
			$post_data ['description'] = '个人无创推广-' . $row['id'];
			$post_data ['link'] = $this->getApiUrl() . 'booking/index?id=1&agent_id='.$id;
			$post_data ['update_time'] = time ();
			
			$post_data ['create_time'] = time ();
			$pid = $model->add ( $post_data );
			// 获取生成二维码的地址
			$qrcod_file = $this->createQrcode ( $pid );
			// 写入二维码图片路径
			$model->modify ( [
					'qrcode_file' => $qrcod_file
			], $pid );
			
			$agentModel->modify(array('scene_id'=>$pid),$id);
			
		}
		
		$response['msg '] = '审核中';
		$response['url'] = '/agent/index';
		$this->forward($response);
	}
	
	
	//详细页面
	public function actionUpdate()
	{
		$agentModel = new AgentModel();
		$id = $this->get('id');
		$info['list'] = $agentModel->getPresentInfo($id);
		return $this->render('update', $info);
	}
	
	
	/**
	 * 
	 */
	protected function getApiUrl()
	{
		if(YII_ENV == 'prod')
			return 'http://wx.zhishimama.com/';
		else 
			return 'http://test.wx.zhishimama.com/';
			
	}
	
	
	/**
	 *
	 * @return array
	 */
	private function getUploadConfig()
	{
		// 上传文件目录
		$path = UPLOAD_PATH . 'upload/';
		// 上传配置
		$config = array (
				"savePath" => $path, // 存储文件夹
				"maxSize" => 100000, // 允许的文件最大尺寸，单位KB
				"allowFiles" => array (
						".gif",
						".png",
						".jpg",
						".jpeg",
						".bmp"
				)
		); // 允许的文件格式
	
	
		// 背景保存在临时目录中
		$config ["savePath"] = $path;
		return $config;
	}
	
	/**
	 * 创建二维码
	 *
	 * @return string path
	 */
	private function createQrcode($id)
	{
		$value = $this->getApiUrl() . 'source-statistics/index?id=' . $id;
		$errorCorrectionLevel = 'L';
		$matrixPointSize = '10';
	
		$basePath = 'qrcode/';
	
		$time = time ();
	
		$filePath = UPLOAD_PATH . $basePath;
		$this->createDir ( $filePath );
		$png_file = $filePath . $time . '.png';
	
		include_once (ROOT_PATH . "/vendor/phpqrcode/phpqrcode.php");
	
		\QRcode::png ( $value, $png_file, $errorCorrectionLevel, $matrixPointSize );
	
		return $basePath . $time . '.png';
	}
	
	/**
	 * 递归创建目录
	 *
	 * @param string $path
	 */
	private function createDir($path)
	{
			if (! file_exists ( $path ))
		{
			$this->createDir ( dirname ( $path ) );
			mkdir ( $path, 0777 );
		}
	}
	
}
